package com.example.demo.connerlt;


import com.alibaba.fastjson.JSON;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@EnableScheduling
public class AlarmTask {

    private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

    /**
     * 定义执行线程启动的开关
     *
     * @throws InterruptedException
     */
//    @Scheduled(cron = "0/6 * * * * *")
//    public void run() throws InterruptedException{
////        Thread.sleep(6000);
//        String time = JSON.toJSONString(format.format(new Date()));
//        System.out.println(Thread.currentThread().getName() + " ==>run1-" +time);
//    }
//
//    /**
//     * 对上一次执行时间之后再执行的时间
//     * @throws InterruptedException
//     */
//    @Scheduled(fixedRate = 5000)
//    public void run1() throws InterruptedException{
////        Thread.sleep(7000);
//        String time = JSON.toJSONString(format.format(new Date()));
//        System.out.println(Thread.currentThread().getName() + " ==>run2-" +time);
//    }

//    @Scheduled(fixedDelay = 5000)
//    public void run2() throws InterruptedException{
////        Thread.sleep(7000);
//        String time = JSON.toJSONString(format.format(new Date()));
//        System.out.println(Thread.currentThread().getName() + " ==>run3-" +time);
//    }
    @Scheduled(fixedDelayString = "5000")
    public void run4() throws InterruptedException {
//        Thread.sleep(7000);
        String time = JSON.toJSONString(format.format(new Date()));
        System.out.println(Thread.currentThread().getName() + " ==>run4-" + time);
    }
}
